package com.shizhanzhe.qdd.forward;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.widget.PopupWindowCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shizhanzhe.qdd.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/11/6.
 */
public class ForwardPopupWindow extends PopupWindow {
    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    private View contextView;
    private Activity mContext;
    private String[] mVals = new String[]
            {"热门排行", "最新", "热门", "养生", "情感", "生活",
                    "美食", "育儿", "搞笑", "时尚", "科技", "职场","明星",
                    "军事", "两性", "佛教", "美女", "小说"};
    private TagAdapter<String> tagAdapter;
    private ForwardListener mListener;

    public void setListener(ForwardListener listener) {
        mListener = listener;
    }

    public ForwardPopupWindow(final Activity context,List<String> names) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contextView = inflater.inflate(R.layout.popup_forward, null);
        mContext = context;
        String[] array = new String[names.size()];
        mVals = names.toArray(array);
        this.setContentView(contextView);
        ButterKnife.bind(this, contextView);
        final LayoutInflater mInflater = LayoutInflater.from(context);
        tagAdapter = new TagAdapter<String>(mVals) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        parent, false);
                tv.setText(s);
                return tv;
            }
        };
        idFlowlayout.setAdapter(tagAdapter);
        tagAdapter.setSelectedList(0);
        idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                mListener.itemCheck(position);
                return true;
            }
        });
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(30000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        //this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
       // this.setAnimationStyle(R.style.my_dialog);
    }

    public void showDown(View view,int position) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 1f;
        mContext.getWindow().setAttributes(lp);
        tagAdapter.setSelectedList(position);
        //this.showAtLocation(view, Gravity.TOP, 0, 50);
        PopupWindowCompat.showAsDropDown(this, view, 0, 0, Gravity.START);
    }

    interface ForwardListener{
        void itemCheck(int itemid);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 1f;
        mContext.getWindow().setAttributes(lp);
    }
}
